/*
 * Created on Nov 9, 2004 by Ming Chow
 *
 * Client for telerobotics sample: starts and stops motor on RCX
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import josx.rcxcomm.RCXBean;

public class MotorControl1Client extends JFrame
{
	private JButton motorActionButton;
	private static final String START_LABEL="Start Motor";
	private static final String STOP_LABEL="Stop Motor";
	
	// buttonState is the flag to start or stop motor
	private static int buttonState;
	
	public static void main(String[] args)
	{
		buttonState=0;
		new MotorControl1Client();
	}
	
	public MotorControl1Client()
	{
		// The user interface consists of only one button
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    Container content = getContentPane();
	    content.setBackground(Color.white);
	    content.setLayout(new FlowLayout());
	    motorActionButton=new JButton(START_LABEL);
	    
	    ActionListener motorActionButtonListener=new
			ActionListener()
			{
	    		public void actionPerformed (ActionEvent e)
	    		{
	    	      try
			      {
	    	      	RCXBean rcxb=new RCXBean();
	    	      	rcxb.setComPort("COM1");
	    			if (buttonState==0)
	    			{
	    				// Send signal to RCX to start the motor...
	    				// ...and change button label
	    				motorActionButton.setLabel(STOP_LABEL);
	    				buttonState=1;
	    				rcxb.sendInt(buttonState);
	    			}
	    			else
	    			{
	    				motorActionButton.setLabel(START_LABEL);
	    				buttonState=0;
	    				rcxb.sendInt(buttonState);
	    			}
	    			rcxb.close();
			      }
	    	      catch (IOException ioe) {};
	    		}
			};
		motorActionButton.addActionListener(motorActionButtonListener);
	    content.add(motorActionButton);
	    pack();
	    setVisible(true);
	}
}
